<template>
    <div>
        <h3>康复理疗列表</h3>
        项目名称
        <input type="text" v-model="type.name">
        <input type="button" value="查询" class="btn btn-success" @click="ShowLine">
        <input type="button" value="批量启用" class="btn btn-info" @click="UpdateState(1)">
        <input type="button" value="批量禁用" class="btn btn-danger" @click="UpdateState(0)">
        <input type="button" value="导出" class="btn btn-warning" @click="xlsx">
        <table class="table table-bordered">
            <tbody>
                <tr class="bg-primary">
                    <input type="checkbox" v-model="checked" @change="Checkedall">
                    <td>项目名称</td>
                    <td>项目编号</td>
                    <td>佣金(元)</td>
                    <td>服务人数</td>
                    <td>服务时长(小时)</td>
                    <td>项目说明</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.lineID">
                    <input type="checkbox" v-model="ids" :value="item.lineID">
                    <td>{{ item.lineName }}</td>
                    <td>{{ item.lineNum }}</td>
                    <td>{{ item.linePrice.toFixed(2) }}</td>
                    <td>{{ item.lineCount }}</td>
                    <td>{{ item.lineTime }}</td>
                    <td>{{ item.lineHome }}</td>
                    <td>{{ item.lineState == 1 ? "启用" : "禁用" }}</td>
                    <td>
                        <input type="button" value="编辑" class="btn btn-success">
                        <input type="button" value="删除" class="btn btn-danger" @click="DelLine(item.lineID)">
                    </td>
                </tr>
            </tbody>
        </table>
        <span>共{{ data.listcount }}条</span>
        <span>每页{{ type.pagesize }}条</span>
        <span>共{{ type.pageindex }}/{{ data.pagecount }}页</span>
        <input type="button" value="首页" @click="page(1)">
        <input type="button" value="上一页" @click="page(type.pageindex - 1)">
        <input type="button" v-for="item in data.pagecount" :value="item" @click="page(item)">
        <input type="button" value="下一页" @click="page(type.pageindex + 1)">
        <input type="button" value="尾页" @click="page(data.pagecount)">
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';
const router = useRouter()
const route = useRoute()
onMounted(() => {
    ShowLine();
})
//批量启用禁用
const UpdateState = (state: any) => {
    if (ids.value.length == 0) {
        alert('至少选一项')
        return
    }
    let f = confirm('确认修改吗?')
    if (f) {
        axios({
            url: '/api/Emp/UpdateAll',
            method: 'put',
            params: {
                ids: ids.value.toString(),
                state: state
            }
        })
            .then((res) => {
                console.log(res);
                if (res.data > 0) {
                    alert('修改成功')
                    checked.value = false
                    ids.value = []
                    ShowLine()
                }
                else {
                    alert('修改失败')
                }
            })
            .catch((err) => {
                console.log(err);
            })
    }
}
//全选
let checked = ref(false);
let ids = ref([]);
const Checkedall = () => {
    if (checked.value) {
        ids.value = data.value.list.map((i: any) => {
            return i.lineID
        })
    }
    else {
        ids.value = []
    }
}
//参数
let type: any = reactive({
    pageindex: 1,
    pagesize: 3,
    name: ''
})
let data: any = ref({
    listcount: '',
    pagecount: '',
    list: []
})
//分页
const page = (num: any) => {
    if (num < 1) {
        type.pageindex = 1
        return
    }
    if (num > data.pagecount) {
        type.pageindex = data.pagecount
        return
    }
    type.pageindex = num
    ShowLine()
}
//显示
const ShowLine = () => {
    axios({
        url: '/api/Emp/ShowLine',
        method: 'get',
        params: type
    })
        .then((res) => {
            console.log(res);
            data.value = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
//删除
const DelLine = (id: any) => {
    let f = confirm('确认删除吗?')
    if (f) {
        axios({
            url: '/api/Emp/DelLine',
            method: 'delete',
            params: {
                id: id
            }
        })
            .then((res) => {
                console.log(res);
                if (res.data > 0) {
                    alert('删除成功')
                    ShowLine()
                }
                else {
                    alert('删除失败')
                }
            })
            .catch((err) => {
                console.log(err);

            })
    }
}
//导出
const xlsx = () => {
    location.href = `https://localhost:7041/api/Emp/Xlsx?pageindex=1&pagesize=100`;
}
</script>

<style scoped></style>